<?php

namespace app\utils;

/**
 * Author:Panda
 * Email:joeyoung0314@qq.com
 * Class AppResult
 * @package App\Utils
 */
class AppResult
{

    /**==============应用返回封装==============**/

    /**
     * 成功返回200
     * @param string $message
     * @param $result
     * @return string json
     */
    public static function response200(string $message = 'success', $result = null): string
    {
        return self::response(200, $message, $result);
    }

    /**
     * 失败返回201
     * @param string $message
     * @param $result
     * @return string json
     */
    public static function response201(string $message = 'error', $result = null): string
    {
        return self::response(201, $message, $result);
    }

    /**
     * 参数缺失返回203
     * @param string $message
     * @param $result
     * @return string json
     */
    public static function response203(string $message = 'error', $result = null): string
    {
        return self::response(203, $message, $result);
    }

    /**
     * 授权无用户返回204
     * @param string $message
     * @param $result
     * @return string json
     */
    public static function response204(string $message = '暂无此用户，请先授权手机号，并登录', $result = null): string
    {
        return self::response(204, $message, $result);
    }

    /**
     * 请求次数过多返回206
     * @param string $message
     * @param $result
     * @return string json
     */
    public static function response206(string $message = '请求次数过多', $result = null): string
    {
        return self::response(206, $message, $result);
    }

    /**
     * 身份信息已过期400
     * @param string $message
     * @param $result
     * @return string json
     */
    public static function response400(string $message = 'Identity is overdue', $result = null): string
    {
        return self::response(400, $message, $result);
    }

    /**
     * 致命错误500
     * @param string $message
     * @param $result
     * @return string json
     */
    public static function response500(string $message = 'Fatal error', $result = null): string
    {
        return self::response(500, $message, $result);
    }

    /**
     * 权限未拥有777
     * @param string $message
     * @param $result
     * @return string json
     */
    public static function response777(string $message = 'No Access', $result = null): string
    {
        return self::response(777, $message, $result);
    }

    /**
     * 返回统一格式
     * @param int $code
     * @param string $message
     * @param $result
     * @return string
     */
    public static function response(int $code, string $message = 'success', $result = null): string
    {
        return json_encode([
            'code'    => $code,
            'message' => $message,
            'result'  => $result
        ], JSON_UNESCAPED_UNICODE);
    }
}